package com.cskaoyan.service;

import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.model.MarketCoupon;
import com.cskaoyan.model.MarketCouponExample;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/6/11
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    MarketCouponMapper couponMapper;
    @Override
    public List<Map> list(int page, int limit) {
        Date current = new Date();
        MarketCouponExample example = new MarketCouponExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andTimeTypeEqualTo((short) 0);
        example.or().andDeletedEqualTo(false)
                .andTimeTypeEqualTo((short)1)
                .andStartTimeGreaterThanOrEqualTo(current)
                .andEndTimeGreaterThanOrEqualTo(current);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(page, limit);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(example);
        List<Map> list = marketCoupons.stream().map(marketCoupon -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", marketCoupon.getId());
            map.put("name", marketCoupon.getName());
            map.put("desc", marketCoupon.getDesc());
            map.put("tag", marketCoupon.getTag());
            map.put("total", marketCoupon.getTotal());
            map.put("discount", marketCoupon.getDiscount());
            map.put("days", marketCoupon.getDays());
            if (marketCoupon.getTimeType().intValue() == 1) {
                map.put("startTime", marketCoupon.getStartTime());
                map.put("endTime", marketCoupon.getEndTime());
            }
            map.put("min", marketCoupon.getMin());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public MarketCoupon getDiscountMoney(Integer couponId) {
        Date current = new Date();
        MarketCouponExample example = new MarketCouponExample();
        example.createCriteria()
                .andIdEqualTo(couponId)
                .andStartTimeLessThanOrEqualTo(current)
                .andEndTimeGreaterThanOrEqualTo(current)
                .andDeletedEqualTo(false);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(example);
        return marketCoupons!=null&&marketCoupons.size()==1?marketCoupons.get(0):null;

    }

    @Override
    public MarketCoupon getOptimalSolution(List<Integer> couponIdlist, BigDecimal checkedGoodsAmount) {
        // 在coupon表中选择满足优惠券最低使用门槛，且优惠力度最大的券Optimal Solution
        BigDecimal maxDiscount=BigDecimal.ZERO;
        Integer BestId = null;
        for (Integer id : couponIdlist) {
            MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(id);
            if (marketCoupon!=null){
                BigDecimal min = marketCoupon.getMin();
                BigDecimal discount = marketCoupon.getDiscount();
                if (checkedGoodsAmount.compareTo(min)>-1){
                    if (maxDiscount.compareTo(discount)==-1){
                        maxDiscount=discount;
                        BestId=id;
                    }
                }
            }

            }
        if (BestId!=null){
            return couponMapper.selectByPrimaryKey(BestId);
        }
        return null;

}}
